package components.ghost;

import images.Images;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Appearance;
import com.uqbar.vainilla.appearances.Sprite;

import components.direction.Direction;
import components.pacman.Pacman;
import components.scenes.Slot;

public class ScapingState extends GhostState {

	private double currentTime;
	private double stopTime;
	
	public ScapingState(Ghost ghost, int forSeconds) {
		super(ghost);
		this.currentTime = 0;
		this.stopTime = forSeconds;
	}
	
	public Direction calculateNextDir(Slot nextSlot) {
		Direction next = findScape(super.calculateNextDir(nextSlot));
		return next;
		
	}
	
	public void update(DeltaState delta){
		this.currentTime += delta.getDelta();
		if (this.currentTime > this.stopTime){
			this.chasing();
		}
	}
	
	public Direction findScape(Direction dir){
		Direction next = dir.nextDir();
		boolean found = false;
		while((!next.equals(dir)) & !found){
			Slot slot = this.getGhost().calculateNextSlot(next);
			if (slot.isWalkeable()){
				found = true;
			}else{
				next = next.nextDir();
			}
		}
		if (found){
			return next;
		} else {
			return dir;
		}
	}

	public double getSpeed() {
		return this.getGhost().getScapingSpeed();
	}

	public void onCollisionWithPacman(Pacman pacman){
		this.getGhost().die();
	}

	public Appearance getAppaearance() {
		return this.getGhost().getScapingSprite();
	}
	
	public Sprite getSprite(Direction dir){
		return Images.getImage("scaping-ghost");
	}
}
